package com.itheima.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 */
public class TencentCOSUtils {
    private  static String secretId = "AKIDUmoa6LJ1RaS3NRFl22mFRNoysUptgDTS";
    private  static String secretKey = "kDPKzOWNyIM9futb2B3f17xXqsd7KHyP";
    private  static String bucketName = "bkt-001-1300605170";
    private  static COSClient cosClient;

    static{
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-nanjing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        cosClient = new COSClient(cred, clientConfig);
    }

    /**
     * 上传文件
     * @param fileComp 文件内容封装
     * {@link COSFileComp}
     * @throws IOException
     */
    public static void upload2COS(COSFileComp fileComp) throws IOException {

        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 设置文件大小
        objectMetadata.setContentLength(fileComp.getLength());
        // 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType(fileComp.getContentType());
        PutObjectResult putObjectResult = cosClient.putObject(bucketName, fileComp.getFileName(), fileComp.getInputStream(), objectMetadata);
        String etag = putObjectResult.getETag();
        System.out.println(etag);

        fileComp.getInputStream().close();
    }

    /**
     * 下载文件
     * @param fileName
     * @param outputFilePath
     * @throws IOException
     */
    public static void download4COS(String fileName,String outputFilePath) throws IOException {
        File downFile = new File(outputFilePath);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileName);
        ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);

    }

    public static void download4COS02() throws IOException {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, "img/60bbc66beb6c4915b4fba7c7ea8e1d6f.jpg");
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        COSObjectInputStream cosObjectInput = cosObject.getObjectContent();
        String crc64Ecma = cosObject.getObjectMetadata().getCrc64Ecma();
        System.out.println(crc64Ecma);
    }

    @Test
    public void down() throws IOException {
        TencentCOSUtils.download4COS02();
    }

}
